package com.zte.market.control;

import com.zte.login.pojo.Users;
import com.zte.market.pojo.Custom;
import com.zte.market.pojo.Order;
import com.zte.market.pojo.Province;
import com.zte.market.pojo.otherPojo.Job;
import com.zte.market.service.CustomService;
import com.zte.market.service.OrderService;
import com.zte.market.service.otherService.JobService;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luode
 * @version 1.0
 * @date 2022/10/01 星期六 10:29
 */
@Controller
@RequestMapping("/market")
public class MarketAction {
    @Autowired
    private CustomService customService; //客户服务

    @Autowired
    private OrderService orderService;  //订单服务

    @Autowired
    private JobService jobService;  //职位 服务


    @RequestMapping("/login")
    public String login() {
        return "/login";
    }

    @RequestMapping("/main")
    public String main() {
        return "/main";
    }

    @RequestMapping("/left")
    public String left() {
        return "left";
    }
    @RequestMapping("/top")
    public String top() {
        return "top";
    }
    @RequestMapping("/footer")
    public String footer() {
        return "footer";
    }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

/*
    @RequestMapping("/customerList")
    public String customerList() {
        return "market/customer/customerList";
    }
*/

    /**
     * 进入客户列表页面
     * @return
     */
    @RequestMapping("/customerList")
    public String customerList(HttpSession session) {
        //获取登录用户的信息
        Users user = (Users) session.getAttribute("user");
        //获取全省列表
        List<Province> provinceList = customService.getAllProvince();
        //获取全部客户信息
        List<Custom> customList = customService.getAllCustom();
        List<Custom> usersCustomList =new ArrayList<>();
        //员工服务的客户
        if(user.getJobId()==3){
            for (int i = 0; i < customList.size(); i++) {
                if(user.getUid() == customList.get(i).getServiceid()){
                    usersCustomList.add(customList.get(i));
                }
            }
            customList = usersCustomList;
        }
        //客户分页
        PageModel<Custom> customPage = customService.queryByPage(customList, 7, 1);
        //通过用户 的jobId获取用户（员工）列表

        session.setAttribute("provinceList",provinceList);
//        session.setAttribute("customList",customList);
        session.setAttribute("customPage",customPage);

        return "market/customer/customerList";
    }

    @RequestMapping("/customerBrowse")
    public String customerBrowse(HttpSession session) {
        //获取登录用户的信息
        Users user = (Users) session.getAttribute("user");
        //获取全省列表
        List<Province> provinceList = customService.getAllProvince();
        //获取全部客户信息
        List<Custom> customList = customService.getAllCustom();
        List<Custom> usersCustomList =new ArrayList<>();
        //员工服务的客户
        if(user.getJobId()==3){
            for (int i = 0; i < customList.size(); i++) {
                if(user.getUid() == customList.get(i).getServiceid()){
                    usersCustomList.add(customList.get(i));
                }
            }
            customList = usersCustomList;
        }
        //客户分页
        PageModel<Custom> customPage = customService.queryByPage(customList, 7, 1);

        session.setAttribute("provinceList",provinceList);
        session.setAttribute("customPage",customPage);
        return "market/customerBrowse/customerBrowse";
    }
    @RequestMapping("/orderList")
    public String orderList(HttpSession session) {
        //获取登录用户的信息
        Users user = (Users) session.getAttribute("user");
        //获取 订单信息
        List<Order> orderList = new ArrayList<>();
        if(user.getJobId() == 3){
            orderList = orderService.getAllByOperatorId(user.getUid());
        }else {
            orderList = orderService.getAll();
        }
        //将订单列表存放在 分页数据中
        PageModel<Order> orderPage = orderService.queryByPage(orderList, 7, 1);
        //获取 项目部(deptId = 1) 中除了 市场组员（job_id = 3）其它职位
        List<Job> jobList = jobService.getJobListByDeptId(1);
        List<Job> jobListTemp = new ArrayList<>();
        for (Job job : jobList) {
            if(job.getJobId() != 3){
                jobListTemp.add(job);
            }
        }

        session.setAttribute("orderPage",orderPage);
        session.setAttribute("jobList",jobListTemp);

        return "market/order/orderList";
    }
    @RequestMapping("/orderExamineList")
    public String orderExamineList(HttpSession session) {

        //获取登录用户的信息
        Users user = (Users) session.getAttribute("user");
        //获取 订单信息
        List<Order> orderList = new ArrayList<>();
        if(user.getDeptId() == 1){
            orderList = orderService.getAllBycheckid(user.getUid());
        }else {
            orderList = orderService.getAll();
        }
        List<Order> newOrderList = new ArrayList<>();
        for (Order order : orderList) {
            if(order.getStatus().equals("审核中")){
                newOrderList.add(order);
            }
        }
        //将订单列表存放在 分页数据中
        PageModel<Order> orderPage = orderService.queryByPage(newOrderList, 7, 1);

        session.setAttribute("orderPage",orderPage);


        return "market/orderExamine/orderExamineList";
    }
}
